//
//  YSMessageDataBase.m
//  YSEmoticonKeyboard
//
//  Created by MOLBASE on 2017/12/21.
//  Copyright © 2017年 YangShen. All rights reserved.
//

#import "YSMessageDataBase.h"
#import "YSChatMessage.h"
#import "FMDB.h"

static FMDatabaseQueue *dataBaseQueue;
static NSString *const tb_message = @"_message";

@implementation YSMessageDataBase

+ (void)initialize {
    dataBaseQueue = [FMDatabaseQueue databaseQueueWithPath:[self getDBPath]];
    
    [dataBaseQueue inDatabase:^(FMDatabase *db) {
        //创建表
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(userId INTEGER PRIMARY KEY, userName TEXT NOT NULL, userHeadImage TEXT NOT NULL, messageContent TEXT NOT NULL, userType INTEGER NOT NULL);", tb_message];
        BOOL createTableResult = [db executeUpdate:sql];
        if (createTableResult) {
            NSLog(@"创建消息表成功");
        }else{
            NSLog(@"创建消息表失败");
        }
    }];
}

+ (BOOL)insertMessageWithSql:(NSString *)sql {
    BOOL __block result;
    
    if (sql.length == 0) {
        return NO;
    }
    
    sql = [NSString stringWithFormat:@"insert or ignore into %@%@", tb_message, sql];
    
    [dataBaseQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"插入成功！");
        }else {
            NSLog(@"插入失败");
        }
    }];
    return result;
}

+ (BOOL)deleteData:(NSString *)deleteSql {
    if (deleteSql == nil) {
        deleteSql = [NSString stringWithFormat:@"DELETE FROM %@",tb_message];
    } else {
        deleteSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@", tb_message, deleteSql];
    }
    BOOL __block result;
    [dataBaseQueue inDatabase:^(FMDatabase *db) {
        
        result = [db executeUpdate:deleteSql];
        if (result) {
            NSLog(@"删除成功！");
        }else {
            NSLog(@"删除失败");
        }
    }];
    
    return result;
}

+ (NSMutableArray *)queryData:(NSString *)querySql {
    if (querySql == nil) {
        querySql = [NSString stringWithFormat:@"SELECT * FROM %@", tb_message];
    } else {
        querySql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@", tb_message, querySql];
    }
    NSMutableArray *arrM = [NSMutableArray array];
    [dataBaseQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *set = [db executeQuery:querySql];
        while ([set next]) {
            YSChatMessage *message = [[YSChatMessage alloc]init];
            message.userName = [set stringForColumn:@"userName"];
            message.userHeadImage = [set stringForColumn:@"userHeadImage"];
            message.userType = [set intForColumn:@"userType"];
            message.messageContent = [set stringForColumn:@"messageContent"];
            message.userId = [set intForColumn:@"userId"];
            [arrM addObject:message];
        }
    }];
    return arrM;
}

+ (NSString *)getDBPath {
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"LiuqsDataBase.sqlite"];
    return filePath;
}
@end
